<?php

namespace app\api\controller\user;

use app\api\controller\Controller;
use app\api\model\User as UserModel;
use app\api\model\Order as OrderModel;
use app\api\model\Setting as SettingModel;
use think\Db;
use think\Session;

/**
 * 个人中心主页
 * Class Index
 * @package app\api\controller\user
 */
class Index extends Controller
{
    /**
     * 获取当前用户信息
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail($mallID=10001)
    {
        // 当前用户信息，带false代表没有点登录按钮的时候调用此接口，会自动弹出大登录页面
        $user = $this->getUser(false);
        $tab= \think\Db::name('wxapp_navbar')->where('wxapp_id', $mallID)->find();
            $copyright=$tab['copyright'];
            $ywd = $tab['ywd'];
        // 订单总数
        $model = new OrderModel;
        return $this->renderSuccess([
            'userInfo' => $user,
            'orderCount' => [
                'payment' => $model->getCount($user, 'payment'),    //未付款
                'delivery' => $model->getCount($user, 'delivery'),  //未发货
                'received' => $model->getCount($user, 'received'),  //运输中
                'comment' => $model->getCount($user, 'comment'),    //已完成
            ],
            'setting' => [
                'points_name' => SettingModel::getPointsName(),
                       'copyright' => $copyright,
                       'ywd'=>$ywd ,//非2是骗审，2是正常
            ],
            'menus' => (new UserModel)->getMenus()   // 个人中心菜单列表
        ]);
    }

    public function detail222($uid=0)
    {
        $userObj = Db::name('user')->where('user_id', $uid)->find();
        return $this->renderSuccess(['userObj'=>$userObj]);
    }

    public function detail333($uid=0)
    {
        $userObj = Db::name('user')->where('user_id', $uid)->find();
        if(empty($userObj['user_id'])){
            return $this->renderError("学号错误，未找到该用户");
        }
        else{
            $jifenArr = Db::name('user_points_log')->where('user_id', '=', $uid)->where('value', '<', '0')->limit(0,20)->order(['log_id'=>'desc'])->select()->toArray();
            return $this->renderSuccess(['userObj'=>$userObj, 'jifenArr'=>$jifenArr]);
        }
    }

    public function duihuanJifen($manUid, $uid,$fen,$beizhu)
    {
        $sql33 = "update ".getTable('user')." set points=points-$fen where user_id=".$uid;
        //return $this->renderError($sql33);
        Db::query($sql33);
        $upArr111['user_id'] = $uid;
        $upArr111['value'] = -$fen;
        $upArr111['remark'] = $beizhu;
        $upArr111['describe'] = $manUid."进行了礼品兑换积分减扣操作";
        $upArr111['wxapp_id'] = $this->wxapp_id;
        $upArr111['create_time'] = time();
        Db::name('user_points_log')->insert($upArr111);
        return $this->renderSuccess(['backok'=>'ok']);
    }

    public function outlogin($uid=0)
    {
        $token = $this->request->param('token');
        if($token){
            Session::clear($token);
        }
        return $this->renderSuccess(['data'=>'outOk']);
    }
}
